

#ifndef _BLE_BLE_H_
#define _BLE_BLE_H_

#include <cmsis_compiler.h>
#include "buf.h"

enum {
	BLE_Cmd_Init = 1,
	BLE_Cmd_Adv = 2,
	BLE_Cmd_Discon = 3,
	BLE_Cmd_GetMac,
	BLE_Cmd_GetVer,
	BLE_Cmd_SetValue,
	BLE_Cmd_GetValue,
	BLE_Cmd_GetGattHandle,
	BLE_Cmd_RequestRssi,
	BLE_Cmd_GetRssi = 0x0A,
	BLE_Cmd_Reset = 0x0B,
	BLE_Cmd_SetMCUInfo = 0x0C,
	BLE_Cmd_DFUBoot = 0x0F,
	BLE_Cmd_DFUpgrade = 0x10,
	BLE_Cmd_DFUFinish = 0x11,
	BLE_Cmd_GetMCUInfo = 0x12,
	BLE_Cmd_SetName = 0x13,
	BLE_Cmd_GetName = 0x14,
	BLE_Cmd_GetState = 0x15,

	BLE_Cmd_HID_GetState = 0x40,
	BLE_Cmd_HID_Transceive = 0x41,
	BLE_Cmd_HID_EOT = 0x42,
	BLE_Cmd_HID_SetMode = 0x43,
};
	


#pragma pack(1)

typedef struct {
	uint8_t ver[20];		//版本号
	uint8_t Model[40];		//模块号
}MCU_Info_T;

typedef struct {
	uint8_t code;
	buffer_t* data;
}BLEPack_T;

/*=====*/

typedef struct {
	uint8_t open;
}BLEInit_Cmd_T;

typedef struct {
	uint16_t id;
	uint8_t length;
	uint8_t data[];
}BLEAdv_Cmd_T;

typedef struct {
	uint16_t handle;
	uint16_t offset;
	uint8_t data[];
}BLESetGatt_Cmd_T;
/*=====*/

/*=====*/

typedef struct {
	uint8_t mac[6];
	uint8_t remoteMac[6];
}BLEGetAddr_Resp_T;

typedef struct {
	uint8_t version[1];
}BLEGetVer_Resp_T;

typedef struct {
	uint8_t data[1];
}BLEGetGatt_Resp_T;

typedef struct {
	uint8_t handle[2];
}BLEGetAttrHandle_Resp_T;

typedef struct {
	uint8_t rssi;
	uint8_t channel;
}BLEGetRssi_Resp_T;

/*=====*/
#pragma pack()

void BLE_Send_Frame(uint8_t code, const uint8_t* buf, uint16_t len);
int BLE_Receive_Frame(uint8_t *code, uint8_t* buf, uint16_t* rlen, int wtime);


int BLE_Command(BLEPack_T* cmd, BLEPack_T* resp, int wtime);
int BLE_Command2(BLEPack_T* cmd, BLEPack_T* resp, int wtime);

/*
参数：state - 0关闭，1开启
*/
int BLE_Init(uint8_t state);

int BLE_GetState(uint8_t* state);

int BLE_SetName(const uint8_t name[], uint8_t length);

int BLE_GetName(uint8_t name[], uint8_t *length);

int BLE_Reset(void);

int BLE_SetMCUInfo(const MCU_Info_T* info);

int BLE_GetMCUInfo(MCU_Info_T* info);

int BLE_DFUBoot(void);

int BLE_DFUpgrade(uint8_t* buf, uint16_t length);

int BLE_DFUFinish(uint8_t buf[], uint8_t len);


/*==========================================================*/

void BLE_Task(void *arg);
int BLE_GetTag(uint8_t* data, uint8_t size);
int BLE_UpgradeService(void);

#endif



